package cemetery.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;


import cemetery.bean.customer;
import cemetery.bean.park;
import cemetery.service.CustomerService;
import cemetery.service.parkService;
import cemetery.service.impl.CustomerServiceImpl;
import cemetery.service.impl.parkServiceImpl;

/**
 * Servlet implementation class parkServket
 */
@WebServlet("/parkServlet")
public class parkServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public parkServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		//action 是请求的操作类别。
		String action=request.getParameter("action");
		parkService newsService=new parkServiceImpl();
		if (action==null || "list".equals(action)) {
			//从请求中拿到数据
			String newsTitle=request.getParameter("name");
			String newsTitles=request.getParameter("names");
			customer customer=new customer();
			customer.setName(newsTitle);
			customer.setYqname(newsTitle);
			//调用业务逻辑层的方法
			List<customer>list=newsService.getList(customer);
			//页面的跳转和数据的设备
			request.setAttribute("newsList", list);
			request.setAttribute("names", newsTitles);
			request.setAttribute("name", newsTitle);
			request.getRequestDispatcher("park.jsp").forward(request, response);
		}
		if ("chaxunfz".equals(action)) {
			customer c=new customer();
		    String name=request.getParameter("name");
		    c.setName(name);

			parkServiceImpl psi=new parkServiceImpl();
			List<customer> list = psi.getList(c);
			Map<String, Object> map = new HashMap<String, Object>();
			if (list != null && list.size() > 0) {
				map.put("code", 0);
				map.put("msg", "成功");
				map.put("data", list.get(0));
			} else {
				map.put("coke", -1);
				map.put("msg", "失败");
			}
			PrintWriter pw = response.getWriter();
			String result = JSON.toJSONString(map);
			pw.write(result);
			pw.flush();// 刷新
			pw.close();
		}
		
		
		if ("update".equals(action)) {
			CustomerService cs=new CustomerServiceImpl();
			String yqname = request.getParameter("yqname");
			String addres = request.getParameter("addres");
			String name = request.getParameter("name");
			String phone = request.getParameter("phone");
			String burial = request.getParameter("burial");
			customer c =new customer();
			c.setYqname(yqname);		
			c.setAddres(addres);
			c.setName(name);
			c.setPhone(phone);
			c.setBurial(Integer.parseInt(burial));
			int count =cs.update(c);
			Map<String, Object> map = new HashMap<String, Object>();
			if (count > 0) {
				map.put("code", 0);
				map.put("msg", "修改成功");

			} else {
				map.put("coke", -1);
				map.put("msg", "修改失败");
			}
			PrintWriter pw = response.getWriter();
			String result = JSON.toJSONString(map);
			pw.write(result);
			pw.flush();// 刷新
			pw.close();
		}
		
	
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
